【问题标题】:How to edit specific portions of the File upload functionality in Shiny?如何在 Shiny 中编辑文件上传功能的特定部分?
【发布时间】:2023-03-14 18:15:01
【问题描述】:

我对 R 编程(以及 StackOverflow 也是)完全陌生,并且正在使用 Shiny 包开发一个小型 R 项目(这似乎更容易并且符合我的要求)。现在我需要上传一个 .csv 文件,Shiny 已经为其提供了模板或以 ui.R 和 server.R 的形式说出代码。但我的问题是,sn-p Shiny 提供的代码包含一个浏览部分以及一个用于标题的复选框部分和用于选择分隔符(逗号、分号、制表符)和引号(无、双、单)的单选按钮。

现在我只需要浏览部分,而不需要标题、分隔符或引号,所以我修改了脚本,但单选按钮似乎没有。

# The code section starts here.
# Original Script provided by Shiny.
ui.R

library(shiny)

 ui <- fluidPage(
 titlePanel("Uploading Files"),
 sidebarLayout(
sidebarPanel(
  fileInput('file1', 'Choose CSV File',
            accept=c('text/csv', 
                     'text/comma-separated-values,text/plain', 
                     '.csv')),
  tags$hr(),
  checkboxInput('header', 'Header', TRUE),
  radioButtons('sep', 'Separator',
               c(Comma=',',
                 Semicolon=';',
                 Tab='\t'),
               ','),
  radioButtons('quote', 'Quote',
               c(None='',
                 'Double Quote'='"',
                 'Single Quote'="'"),
               '"')
),
mainPanel(
  tableOutput('contents')
  )
 )
)


 server.R file

 library(shiny)

 server <- function(input, output) {
 output$contents <- renderTable({

# input$file1 will be NULL initially. After the user selects
# and uploads a file, it will be a data frame with 'name',
# 'size', 'type', and 'datapath' columns. The 'datapath'
# column will contain the local filenames where the data can
# be found.

inFile <- input$file1

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

read.csv(inFile$datapath, header=input$header, sep=input$sep, 
         quote=input$quote)
    })
    }

shinyApp(ui=ui, server = server)

我能够删除标题复选框部分,如第二张图片所示

但无法移除单选框。请告知如何做到这一点。提前感谢大家。

【问题讨论】:

    标签: r shiny data-science


    【解决方案1】:

    我不知道我是否正确理解了您的问题,但您只需从ui 中删除两个radioButtons()checkboxInput()

    ui <- fluidPage(
      titlePanel("Uploading Files"),
      sidebarLayout(
        sidebarPanel(
          fileInput('file1', 'Choose CSV File',
                    accept=c('text/csv', 
                             'text/comma-separated-values,text/plain', 
                             '.csv')),
          tags$hr()
        ),
        mainPanel(
          tableOutput('contents')
        )
      )
    )
    

    然后从read.csv() 中的server 中删除或替换三个输入变量(input$headerinput$setinput$quote):

    server <- function(input, output) {
      output$contents <- renderTable({
    
        inFile <- input$file1
    
        if (is.null(inFile))
          return(NULL)
    
        read.csv(inFile$datapath, header=T, sep=";")
      })
    }
    

    【讨论】:

    • 非常感谢。我正在删除 ui.R 中的单选按钮部分,但正在将 server.R 中的 read.csv 部分修改为 read.csv(inFile$datapath)。也许这就是我没有得到想要的输出的原因。再次感谢您的及时和宝贵的建议。它就像一个魅力。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-11
    相关资源
    最近更新 更多