【问题标题】:FileInput Button with selectInput in shiny..!带有 selectInput 的 FileInput 按钮闪亮..!
【发布时间】:2017-09-10 12:08:40
【问题描述】:

我正在尝试从上传到应用程序的“汽车”数据集中获取速度变量。基本上在选择速度下:我希望所有数字都出现在数据集$速度中。在 selectInput 下,选择应该取决于我使用 fileInput 上传的数据集。我怎样才能完成这个任务。现在我已将选项添加为 1、2、3。理论上应该有汽车数据集的速度变量的所有值。

library(shiny)
library(datasets)
##the file I am uploading 
data(cars)
dataset=write.csv(cars, "dataset.csv")


ui=fluidPage( 

actionButton("upload", "Upload File"),
bsModal("uploadFile", " ", "upload", 
sidebarLayout(
sidebarPanel(
fileInput("file","Choose file to upload")

),
mainPanel(
tableOutput("contents")
)
)
),  



sidebarLayout(

sidebarPanel(

column(3, selectInput("selectElement", "Select speed:", c(1,2,3),multiple = 
T, selectize = F)

)
),

mainPanel(
)
)
)

server=function(input,output,session){

output$contents <- renderTable({

inFile <- input$file

if (is.null(inFile))
return(NULL)

read.csv(inFile$datapath)
})
}

shinyApp(ui,server)

【问题讨论】:

    标签: shiny rstudio


    【解决方案1】:

    对于有些不完整的回复,我提前道歉:见下文。

    首先,回答您的问题:

    如果你有一个像汽车这样的数据集,你可以这样做来识别“速度”标签:

    labls <- unique(cars$speed)
    ...
    selectInput("selectElement", "Select speed:", labls, multiple = 
                              T, selectize = F)
    

    我希望发布一个完整的示例,但当前的逻辑(可能是因为发布的代码有限?)似乎不正确:应用程序如何 a) 让用户选择要使用的文件;同时b)已经过滤了速度? 当然,您可能计划显示包含所有名为“速度”的列的数据集,那么这将是有意义的:)

    此外,但这不是您的问题的一部分,您似乎通过包 shinyBS 使用模态对话。

    从 Shiny 的 0.14 版本(大约 2016 年 10 月左右)开始,shiny 具有非常好的模态功能,我个人认为使用原生功能会更好。

    我想发布一个从您的代码派生的简单示例(但将 selectInput for speed 注释掉,因为如前所述,它在发布示例的上下文中显示不正确)。

    library(shiny)
    library(datasets) 
    data(cars)
    dataset = write.csv(cars, "dataset.csv")  
    
    labls <- unique(cars$speed) # I left this in the code
    
    ui=fluidPage( 
      sidebarLayout(
      sidebarPanel(
             actionButton("upload", "Upload File") 
        ),
    
      mainPanel(tableOutput("contents") )
    ))
    server=function(input,output,session){
    # Show modal when button is clicked.
    observeEvent(input$upload, {
      showModal(dataModal())
      })
    dataModal <- function(failed = FALSE) {
      modalDialog(
        fileInput('inputId', label=NULL, multiple = FALSE, accept = NULL, width =      NULL, buttonLabel = "Browse...", placeholder = "No file selected")
    # , selectInput("selectElement", "Select speed:", labls, multiple = 
    #                               T, selectize = F)
      )
    }  
    output$contents <- renderTable({
    if (length(input$inputId )== 0) return(NULL) 
        inFile <- input$inputId   
    # if (is.null(input$selectElement )) return(NULL)    
    input$inputId 
    })
    }
    
    shinyApp(ui,server)
    

    【讨论】:

    • 它对我有用。我正在使用 Chrome。从 sessionInfo() 中提取。请检查您是否有相同的版本(尤其是闪亮的)。 >sessionInfo()R 版本 3.3.3 (2017-03-06) (...) 其他附加包:[1] shiny_1.0.1 通过命名空间加载(未附加):[1] R6_2.2.0 htmltools_0.3.5 tools_3.3.3 Rcpp_0.12.10 [5] jsonlite_1.4 digest_0.6.12 xtable_1.8-2 httpuv_1.3.3 [9] mime_0.5
    猜你喜欢
    • 2019-01-24
    • 2015-05-07
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    • 2018-08-24
    • 2016-04-09
    • 2016-10-08
    • 2019-03-20
    相关资源
    最近更新 更多