R 从用户界面更改变量类

R 从用户界面更改变量类,r,shiny,shinydashboard,R,Shiny,Shinydashboard,我的目标是更改上传的数据集的变量数据类数据类应由用户在ui中选择 由于上载的数据集可能不同,我使用lappy为数据集中的每一列创建selectInput。下面的一段代码为每个名为variable_i的列生成selectInputs,其中i是一个列数 lapply(seq(ncol( rawdata() )),function(i){ selectInput(inputId = paste0("variable","_",i),label = colnames(rawda

我的目标是更改上传的
数据集的变量
数据类
<代码>数据类应由用户在
ui
中选择

由于上载的
数据集
可能不同,我使用
lappy
为数据集中的每一列创建
selectInput
。下面的一段代码为每个名为
variable_i
的列生成selectInputs,其中
i
是一个列数

lapply(seq(ncol( rawdata() )),function(i){

            selectInput(inputId = paste0("variable","_",i),label = colnames(rawdata())[i],
                        choices = c("factor", "numeric", "integer", "character"),
                        selected = class(rawdata()[,i])
            )
          })
使用此
选择输入
用户可以输入
数据类
。 我的问题是,如何使用输入信息从
input$variable\u i
更改变量类,因为输入名称是动态的,我不能简单地调用它

也许还有其他方法可以从UI更改数据类吗

我在下面使用的全部代码。如果问题得到解决,我将分享完整的工作代码

library(summarytools)
library("shiny")
library("shinydashboard")
library("dplyr")
# Server ------------------------------------------------------------------

server <- function(input, output,session) {


#upload file from PC
rawdata <- reactive({

    inFile <- input$fileIn

    if (is.null(inFile))
        return(NULL)
        read.csv(inFile$datapath, sep=input$Separator)

  })




#Get data example
output$DataCheck <- renderTable({

                      head(rawdata())

                      })



#get data summary
output$summaryTable <- renderUI({

  out <- print(dfSummary( rawdata(),graph.magnif = 0.8), style = 'grid', omit.headings = TRUE, method = 'render',bootstrap.css = FALSE)
  out[[3]][[2]][[1]]
})



output$colname_in <- renderUI({

  selectInput(inputId = "colname",
              label = "Choose column",
              choices = c("",colnames(rawdata())),
              selected = "")

})



observeEvent(input$change_class, {

  v$data <- eval(parse(text = paste0('v$data %>% mutate(',
                                     input$colname,
                                     ' = as.',
                                     input$class,
                                     '(',
                                     input$colname,
                                     '))')
  )
  )

})


}
# UI ----------------------------------------------------------------------

ui <- bootstrapPage(

  dashboardPage(

    dashboardHeader(title = "AK47"
                    ), #dashboardHeader

 #Sidebar--------------------------------------------------------------------------

  dashboardSidebar(

      sidebarMenu(
                   id = "tabs",
                   menuItem("Step 1: Input Data", tabName = "Input", icon = icon("cog"))

                  ) #sidebarMenu

    ),#dashboardSidebar



 #Body-------------------------------------------------------------------------------------

 dashboardBody(

   tabItems(

     tabItem("Input",

           fluidRow(  
             box(width = 4,title = 'Upload your data file:',solidHeader = T,status = 'primary',


                 #Upload file from PC
                 fileInput('fileIn',label = 'Select the data'),

                 #choose separator
                 radioButtons("Separator","Choose separator:", inline = TRUE,
                              choices=c(Comma=",",
                                        Semicolon=";",
                                        Tab="\t"),  selected = ";")

             ) #box
           ), #fluidRiw


           fluidRow(  
             box(width = 12,title = 'Check the data for correct separator:',solidHeader = F ,status = 'primary',

                 tableOutput('DataCheck'),
                 tags$hr()

             ) #box
           ),  #fluidRow

           fluidRow(  
             box(width = 12,title = 'Change variable typesr:',solidHeader = F ,status = 'primary',

                 uiOutput("colname_in"),

                 selectInput(inputId = "class",
                             label = "Choose class",
                             choices = c("", "factor", "numeric", "integer", "character"),
                             selected = ""),

                 actionButton("change_class",
                              "Change class"),

                 uiOutput('summaryTable'),

                 tags$hr()

             ) #box
           )  #fluidRow


     ) #tabItem



   ) #tabItems


      )#dashboardBody  




  )#dashboardPage
)#bootstrapPage

shinyApp(ui, server)
库(摘要工具)
图书馆(“闪亮”)
图书馆(“shinydashboard”)
图书馆(“dplyr”)
#服务器------------------------------------------------------------------

服务器不是为每列生成一个
selectInput
,而是使用两个
selectInputs
,一个用于列名,一个用于类,还有一个操作按钮来更改类

这是我的方法。这可能并不完美,但可能是一个开始

library("shiny")
library("dplyr")
library("ggplot2")


shinyApp(

    ui = fluidPage(

        # Layout with sidebar
        sidebarLayout(

            ## Sidebar -----
            sidebarPanel(

                # > some example input on sidebar -----

                uiOutput("colname_in"),
                selectInput(inputId = "class",
                            label = "Choose class",
                            choices = c("", "factor", "numeric", "integer", "character"),
                            selected = ""),
                actionButton("change_class",
                             "Change class")

            ), # closes Sidebar-Panel



            # Main-Panel ------
            mainPanel(


                             tableOutput("print")





            )  # closes mainPanel                      

        ) # closes sidebarLayout

    ), # closes fluidPage


    # Server ------
    server = function(input, output, session){

        v <- reactiveValues(data = iris)


        output$colname_in <- renderUI({

            selectInput(inputId = "colname",
                        label = "Choose column",
                        choices = c("",colnames(v$data)),
                        selected = "")

        })

        observeEvent(input$change_class, {

            v$data <- eval(parse(text = paste0('v$data %>% mutate(',
                                     input$colname,
                                     ' = as.',
                                     input$class,
                                     '(',
                                     input$colname,
                                     '))')
                               )
                 )

        })



        output$print <- renderTable({

            print(input$colname)
            print(input$class)
            print(input$change_class)

                v$data

        })

    } # Closes server
) # Closes ShinyApp
库(“闪亮”)
图书馆(“dplyr”)
图书馆(“ggplot2”)
shinyApp(
ui=fluidPage(
#带边栏的布局
侧边栏布局(
##边栏-----
侧栏面板(
#>侧栏上的一些示例输入-----
uiOutput(“colname_in”),
选择输入(inputId=“class”,
label=“选择类”,
选项=c(“,”因子“,”数字“,”整数“,”字符“),
已选“”),
actionButton(“更改类”,
“更改类别”)
),#关闭侧栏面板
#主面板------
主面板(
表格输出(“打印”)
)#关闭主面板
)#关闭侧边栏布局
),#关闭fluidPage
#服务器------
服务器=功能(输入、输出、会话){

v我看到你使用了
v我认为是赋值导致了你的反应式出现问题。你不能给反应式赋值,但你可以将值写入反应式值。将反应式更改为反应式值不会导致你的代码出现问题。我无法使用你的示例。也许我遗漏了什么?我的目标是获取最终的
dataframe
,它将用于其他计算。请参阅我的完整代码以进行数据读取和检查:是否有解决方案?非常好的工作,非常感谢@TimTeaFan!稍微更新您的代码,以使
分隔符的选择正常工作。
library("summarytools")
library("shiny")
library("shinydashboard")
library("dplyr")
# Server ------------------------------------------------------------------

server <- function(input, output,session) {


    #upload file from PC
    v = reactiveValues(path = NULL)


    observeEvent(input$fileIn, {
        req(input$fileIn)
        v$data <- read.csv(input$fileIn$datapath, sep = input$Separator)


    })




    #Get data example
    output$DataCheck <- renderTable({

        req(v$data)
        head(v$data)

    })



    #get data summary
    output$summaryTable <- renderUI({

        req(v$data)

        out <- print(dfSummary(v$data,graph.magnif = 0.8), style = 'grid', omit.headings = TRUE, method = 'render',bootstrap.css = FALSE)
        out[[3]][[2]][[1]]

    })



    output$colname_in <- renderUI({

        req(v$data)

        selectInput(inputId = "colname",
                    label = "Choose column",
                    choices = c("",colnames(v$data)),
                    selected = "")

    })



    observeEvent(input$change_class, {

        v$data <- eval(parse(text = paste0('v$data %>% mutate(',
                                           input$colname,
                                           ' = as.',
                                           input$class,
                                           '(',
                                           input$colname,
                                           '))')
        )
        )

    })


}
# UI ----------------------------------------------------------------------

ui <- bootstrapPage(

    dashboardPage(

        dashboardHeader(title = "AK47"
        ), #dashboardHeader

        #Sidebar--------------------------------------------------------------------------

        dashboardSidebar(

            sidebarMenu(
                id = "tabs",
                menuItem("Step 1: Input Data", tabName = "Input", icon = icon("cog"))

            ) #sidebarMenu

        ),#dashboardSidebar



        #Body-------------------------------------------------------------------------------------

        dashboardBody(

            tabItems(

                tabItem("Input",

                        fluidRow(  
                            box(width = 4,title = 'Upload your data file:',solidHeader = T,status = 'primary',


                                #Upload file from PC
                                fileInput('fileIn',label = 'Select the data'),

                                #choose separator
                                radioButtons("Separator","Choose separator:", inline = TRUE,
                                             choices=c(Comma=",",
                                                       Semicolon=";",
                                                       Tab="\t"),  selected = ";")

                            ) #box
                        ), #fluidRiw


                        fluidRow(  
                            box(width = 12,title = 'Check the data for correct separator:',solidHeader = F ,status = 'primary',

                                tableOutput('DataCheck'),
                                tags$hr()

                            ) #box
                        ),  #fluidRow

                        fluidRow(  
                            box(width = 12,title = 'Change variable typesr:',solidHeader = F ,status = 'primary',

                                uiOutput("colname_in"),

                                selectInput(inputId = "class",
                                            label = "Choose class",
                                            choices = c("", "factor", "numeric", "integer", "character"),
                                            selected = ""),

                                actionButton("change_class",
                                             "Change class"),

                                uiOutput('summaryTable'),

                                tags$hr()

                            ) #box
                        )  #fluidRow


                ) #tabItem



            ) #tabItems


        )#dashboardBody  




    )#dashboardPage
)#bootstrapPage

shinyApp(ui, server)