After browse data it not displayed in shiny dashboard

我与影子孤独终老i 提交于 2020-01-16 09:05:11

问题


I want to upload a file using "Browse option" after that, I want to do autofill of table using "WorklistNo"(means: choosing one input should fill rest field automatically) values and rest column should be auto fill. But I am getting some errors. Based on WorklistNo my other data should be populated in different input boxes in the shiny dashboard.

[![enter image description here][1]][1]library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Basic dashboard"),
  dashboardSidebar(),
  dashboardBody(
    fluidRow(
      fileInput("file_upload","Uplaod Data",buttonLabel = "Browse..",placeholder = "No file Selected")
    ),
    fluidRow(
      column(3,
             uiOutput("p1_ui")
      ),
      column(3,
             uiOutput("p2_ui")
      ),
      column(3,
             uiOutput("p3_ui")
      )
    ),
    fluidRow(
      column(6,
             h3("Uploaded DATA"),
             DTOutput("uploaded_data_table")
      ),
      column(6,
             h3("Selected DATA"),
             DTOutput("selected_data_table")
      )
    )
  )
)

server <- function(input, output,session) {
  values <- reactiveValues(
    upload_state = NULL
  )
  data_upload_fun<-eventReactive(input$file_upload,{
    req(input$file_upload)
    if(values$upload_state=='reset'||is.null(values$upload_state))
    {
      data_in<-read_xlsx(input$file_upload$datapath
                    )
      values$upload_state <- 'uploaded'
      data_in
    }  
  })
  output$uploaded_data_table <- renderDT({
    DT::datatable(data_upload_fun())
  })
  output$p1_ui<-renderUI({
    if(is.null(values$upload_state)|| values$upload_state=='reset')
    {
      selectInput("p1", choices = NULL, label = 'WorklistNo')
   } 
    else
    {
      data_upload_fun()
      selectInput("p1", choices = uploade_data$WorklistNo, label = 'WorklistNo')
    }
  })

  output$p2_ui<-renderUI({
    if(is.null(values$upload_state)|| values$upload_state=='reset')
    {
      selectInput("p2", choices = NULL, label = 'Status')
    }
    else
    {
      data_upload_fun()
      status<-data_in[data_in$WorklistNo==input$p1,2]
      selectInput("p2", choices = as.list(status), label = 'Status')
    }
  })
  output$p3_ui<-renderUI({
    if(is.null(values$upload_state)|| values$upload_state=='reset')
    {
      selectInput("p3", choices = NULL, label = 'Plant')
    }
    else
    {
      data_upload_fun()
      plant<-data_in[data_in$WorklistNo==input$p1 & data_in$Status==input$p2,3]
      selectInput("p3", choices = as.list(plant), label = 'Plant')
    }
  })

  output$selected_data_table<-renderDT({
    if(is.null(values$upload_state)|| values$upload_state=='reset')
    {
      returnValue()
    }
    else
    {
      data_upload_fun()
      data_to_show<-data_in[data_in$WorklistNo==input$p1 & data_in$Status==input$p2 & data_in$Plant== input$p3, ]
      DT::datatable(data_to_show)
    }
  })

}

shinyApp(ui, server) 


回答1:


from the previous question it seems you want to do this reactively. You need to understand the concept of Shiny Reactive component Behavior.

However, Few points need to highlight.

  • Make 'data_in<-read_xlsx()' as 'data_in<<-read_xlsx()' to make it global assignment.
  • In output$pi_ui you have used 'uploade_data$WorklistNo' which will be 'data_in$WorklistNo'
  • I think you are uploading a static excel file. So you need to modify the code according your excel. My code was running on based on my excel. So to make it working you need to change accordingly.

    However you need to give a minimal reproducible code even you want to work with your data then kindly provide minimal data set.

Revised Code with same dataset(.xlsx file)

library(shinydashboard)
library(DT)
library(xlsx)

ui <- dashboardPage(
  dashboardHeader(title = "Basic dashboard"),
  dashboardSidebar(),
  dashboardBody(
    fluidRow(
      fileInput("file_upload","Uplaod Data",buttonLabel = "Browse..",placeholder = "No file Selected")
    ),
    fluidRow(
      column(3,
             uiOutput("p1_ui")
      ),
      column(3,
             uiOutput("p2_ui")
      ),
      column(3,
             uiOutput("p3_ui")
      )
    ),
    fluidRow(
      column(6,
             h3("Uploaded DATA"),
             DTOutput("uploaded_data_table")
      ),
      column(6,
             h3("Selected DATA"),
             DTOutput("selected_data_table")
      )
    )
  )
)

server <- function(input, output,session) {
  values <- reactiveValues(
    upload_state = NULL
  )
  data_upload_fun<-eventReactive(input$file_upload,{
    req(input$file_upload)
    if(values$upload_state=='reset'||is.null(values$upload_state))
    {
      data_in<<-read.xlsx(input$file_upload$datapath,1)
      values$upload_state <- 'uploaded'
      data_in
    }  
  })


  output$uploaded_data_table <- renderDT({
    DT::datatable(data_upload_fun())
  })
  output$p1_ui<-renderUI({
    if(is.null(values$upload_state)|| values$upload_state=='reset')
    {
      selectInput("p1", choices = NULL, label = 'WorklistNo')
    } 
    else
    {
      data_upload_fun()
      selectInput("p1", choices = data_in$WLID, label = 'WorklistNo')
    }
  })

  output$p2_ui<-renderUI({
    if(is.null(values$upload_state)|| values$upload_state=='reset')
    {
      selectInput("p2", choices = NULL, label = 'Status')
    }
    else
    {
      data_upload_fun()
      status<-data_in[data_in$WLID==input$p1,3]
      selectInput("p2", choices = as.list(status), label = 'Status')
    }
  })
  output$p3_ui<-renderUI({
    if(is.null(values$upload_state)|| values$upload_state=='reset')
    {
      selectInput("p3", choices = NULL, label = 'Plant')
    }
    else
    {
      data_upload_fun()
      plant<-data_in[data_in$WLID==input$p1 & data_in$STATUS==input$p2,2]
      selectInput("p3", choices = as.list(plant), label = 'Plant')
    }
  })

  output$selected_data_table<-renderDT({
    if(is.null(values$upload_state)|| values$upload_state=='reset')
    {
      returnValue()
    }
    else
    {
      data_upload_fun()
      data_to_show<-data_in[data_in$WLID==input$p1 & data_in$STATUS==input$p2 & data_in$PLANT== input$p3, ]
      DT::datatable(data_to_show)
    }
  })

}

shinyApp(ui, server) 

Hope this helps...




回答2:


When you call a reactive, you can't reference something that's been created within it - you reference the reactive. So where you reference data_in, you should be using data_upload_fun().

The other issue is that uploade_data doesn't seem to be a dataset in your code so you have possibly made a typo there or have accidentally not included a dataset.

I can't run your code so I'm not totally sure if that will fix everything but it deals with some of the issues.



来源:https://stackoverflow.com/questions/56927812/after-browse-data-it-not-displayed-in-shiny-dashboard

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!